require 'yajl'

class Television < ActiveRecord::Base
  has_paper_trail :version => :paper_trail_version, :versions => :paper_trail_versions
  has_many :programs
  attr_protected

  #validates :play_url, :uniqueness => true
  validates :name, :uniqueness => true

  default_scope order("name asc")

  def guides_filename
    "#{Time.now.stamp('2013-02-28')}.json"
  end


  def guides
    if File.exist?("#{Rails.root.to_s}/public/guides/#{self.guides_filename}")
      json = File.new("#{Rails.root.to_s}/public/guides/#{self.guides_filename}", 'r')
      parser = Yajl::Parser.new
      hash = parser.parse(json)
      return hash[self.id.to_s]
    end
  end

  alias_method :has_guide?, :guides

end
